
import fs from "fs";
import getLocalConfig from "./getLocalConfig";
import initFolder from "./initFolder";

async function writeFile(_, fileName, content) {
    let { 'download-folder': downloadPath } = await getLocalConfig();
    await initFolder(downloadPath);
    if (!downloadPath.endsWith('/')) {
        downloadPath = downloadPath + '/';
    }
    const filePath = downloadPath + fileName;
    return new Promise((resolve) => {
        fs.writeFile(filePath, content, (error) => {
            if (error) {
                resolve({
                    status: false,
                    filePath,
                    msg: error,
                })
            } else {
                resolve({
                    status: true,
                    filePath,
                    msg: '',
                })
            }
        });
    })
}

export default writeFile;